package com.qf.pkg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.pkg.entity.Package;
import com.qf.pkg.mapper.PackageMapper;
import com.qf.pkg.service.PackageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 租户套餐表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-10-27
 */
@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Override
    @Transactional
    public R add(Package pck) {
        if (pck == null) {
            return R.fail();
        }
        pck.setCreator("1");
        pck.setCreateTime(new Date());
        pck.setUpdater("1");
        pck.setUpdateTime(new Date());
        pck.setStatus(0);
        pck.setDeleted(0);
        this.baseMapper.insert(pck);


        return R.ok();
    }

    /**
     * 套餐分页查询
     * @param pageDto
     * @return
     */
    @Override
    public PageResult<Package> selectPage(PageDto<Package> pageDto) {
        IPage<Package> page = new Page<>(pageDto.getNow(),pageDto.getSize());

        //分页条件
        QueryWrapper<Package> queryWrapper = new QueryWrapper<>();
        //判断是否为空
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())) {
            queryWrapper.lambda().like(Package::getName,pageDto.getWhere().getName());
        }
        IPage<Package> pageInfo = this.baseMapper.selectPage(page, queryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setList(pageInfo.getRecords());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());

        return pageResult;
    }
}
